/*
 * Copyright 2010 Pierre Heinzerling, Uwe Janner, Patrick Wright
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.upp.publinksky.model;

import com.google.appengine.api.datastore.Key;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;

/**
 * a link submitted to a share; composite key is share + url
 */
@PersistenceCapable
@XmlRootElement(name = "link")
public class Link {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private Key theme;

    private int themeId;
    @Persistent
    private String title;
    @Persistent
    private String uriString;
    @Persistent
    private Date addedOn;
    @Persistent
    private String addedByEmail;
    @Persistent
    private Integer score;
    @Persistent
    private Integer visitCnt;

    public Link() {
        score = 1;
    }


    public Key getKey() {
        return key;
    }

    public void setKey(final Key key) {
        this.key = key;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(final String title) {
        this.title = title;
    }

    public String getUriString() {
        return uriString;
    }

    public void setUriString(final String uriString) {
        this.uriString = uriString;
    }

    public Date getAddedOn() {
        return addedOn;
    }

    public void setAddedOn(final Date addedOn) {
        this.addedOn = addedOn;
    }

    public String getAddedByEmail() {
        return addedByEmail;
    }

    public void setAddedByEmail(final String addedByEmail) {
        this.addedByEmail = addedByEmail;
    }

    public Key getTheme() {
        return theme;
    }

    public void setTheme(final Key theme) {
        this.theme = theme;
    }

    public int getThemeId() {
        return themeId;
    }

    public void setThemeId(final int themeId) {
        this.themeId = themeId;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(final Integer score) {
        this.score = score;
    }

    public Integer getVisitCnt() {
        return visitCnt;
    }

    public void setVisitCnt(final Integer visitCnt) {
        this.visitCnt = visitCnt;
    }

    public void visited() {
        setVisitCnt(getVisitCnt() + 1);
    }

    public void reduceScore() {
        setScore(score > 0 ? score - 1 : 0);
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof Link)) return false;

        final Link link = (Link) o;

        if (key != null ? !key.equals(link.key) : link.key != null) return false;
        if (theme != null ? !theme.equals(link.theme) : link.theme != null) return false;
        if (uriString != null ? !uriString.equals(link.uriString) : link.uriString != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = key != null ? key.hashCode() : 0;
        result = 31 * result + (theme != null ? theme.hashCode() : 0);
        result = 31 * result + (uriString != null ? uriString.hashCode() : 0);
        return result;
    }
}
